package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class UserServiceImpl implements UserService {


    public User login(User user) {
        // 调用dao查询

        try {
            //1. 读取核心配置文件
            InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");

            //2. 获取工厂
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

            //3. 获取SqlSession对象
            SqlSession sqlSession = factory.openSession();

            //4. 获取dao代理对象
            UserDao userDao = sqlSession.getMapper(UserDao.class);

            //5. 调用登录方法
            User uu = userDao.login(user);

            //6. 释放资源
            sqlSession.close();

            //7. 返回查询的User对象
            return uu;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
